import 'package:flutter/material.dart';
import 'package:interview/components/app/app_ripple_button.dart';
import 'package:interview/theme/app_colors.dart';

class AppConfirmDialog {
  /// 展示弹框
  static Future<bool?> show({
    required BuildContext context,
    required AppColors theme,
    required Widget content,
    required String confirmText,
    String? title,
    bool showCloseIcon = false,
    bool maskClick = false,
    double borderRadius = 12.0,
    String? cancelText,
    VoidCallback? onClose,
    VoidCallback? onCancel,
  }){
    return showGeneralDialog<bool>(
      context: context,
      barrierDismissible: maskClick,
      barrierLabel:MaterialLocalizations.of(context).modalBarrierDismissLabel,
      barrierColor: Colors.black.withOpacity(0.7),
      transitionDuration: const Duration(milliseconds: 240),
      pageBuilder:(context , animation , secondaryAnimation){
        return Align(
          alignment: Alignment.bottomCenter,
          child: Material(
            color: Colors.transparent,
            child: Dialog(
              child:  Container(
                width: MediaQuery.of(context).size.width * 0.8,
                constraints: BoxConstraints(
                  maxHeight: MediaQuery.of(context).size.height * 0.8,
                ),
                decoration: BoxDecoration(
                  color:theme.contrastMix2! ,
                  borderRadius: BorderRadius.circular(borderRadius)
                ),
                child: Stack(
                  children: [
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        if (title != null && title.isNotEmpty)
                          Padding(
                            padding: const EdgeInsets.only(top: 16.0,),
                            child: Text(
                              title,
                              style:TextStyle(
                                fontSize: 14,
                                fontWeight: FontWeight.bold,
                                color: theme.contrastPure
                              ),
                              textAlign: TextAlign.center,
                            ),
                          ),
                        Padding(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 16.0,
                            vertical: 16.0,
                          ),
                          child: content,
                        ),
                        _buildButtons(
                          context: context,
                          theme:theme,
                          confirmText: confirmText,
                          cancelText: cancelText,
                          onCancel: onCancel,
                        ),
                      ],
                    ),
                    if (showCloseIcon)
                      Positioned(
                        top: 0,
                        right: 0,
                        child: Material(
                          color: Colors.transparent,
                          child: IconButton(
                            icon: Icon(Icons.close, color: theme.contrastPure , size: 10,),
                            onPressed: () {
                              if (onClose != null) {
                                Navigator.of(context).pop();
                                onClose();
                              } else {
                                Navigator.of(context).pop(false);
                              }
                            },
                          ),
                        ),
                      ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
      transitionBuilder: (context, animation, secondaryAnimation, child) {
        // 淡入淡出动画
        final fadeAnimation = CurvedAnimation(
          parent: animation,
          curve: Curves.easeInOut, // 淡入淡出的缓动曲线
        );
        // 缩放动画
        final scaleAnimation = Tween<double>(
          begin: 0.7, // 初始缩小
          end: 1.0, // 最终正常大小
        ).animate(CurvedAnimation(
          parent: animation,
          curve: Curves.easeOutCubic, // 缓动曲线
        ));
        return FadeTransition(
          opacity: fadeAnimation,
          child: ScaleTransition(scale: scaleAnimation , child: child,),
        );
      },
    );
  }


  static Widget _buildButtons({
    required BuildContext context,
    required String confirmText,
    String? cancelText,
    VoidCallback? onCancel,
    AppColors? theme
  }) {
    if (cancelText == null) {
      // 单按钮布局
      return Container(
        decoration: const BoxDecoration(
          border: Border(
            top: BorderSide(color: Color(0xFFCCCCCC), width: 1),
          ),
        ),
        child: TextButton(
          onPressed: () {
            Navigator.of(context).pop(true);
          },
          child: Text(
            confirmText,
            style: TextStyle(
              fontSize: 12,
              fontWeight: FontWeight.w600,
              color: theme?.subjectPure,
            ),
          ),
        ),
      );
    }

    // 双按钮布局
    return Container(
      decoration: const BoxDecoration(
        border: Border(
          top: BorderSide(color: Color(0xFFCCCCCC), width: 0.5),
        ),
      ),
      child: Row(
        children: [
          Expanded(
            child: AppRippleButton(
              onTap: (){ 
                Navigator.of(context).pop(true); 
              },
              padding: EdgeInsets.symmetric(vertical: 12),
              child: Center(child: 
                Text(
                  confirmText,
                  style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.w600,
                    color: theme?.contrastPure,
                  ),
                ),
              )
            ),
          ),
          Container(
            width: 0.5, // 分割线宽度
            height: 40,
            color: const Color(0xFFCCCCCC), // 分割线颜色
          ),
          Expanded(
             child: AppRippleButton(
              onTap: (){ 
                if (onCancel != null) {
                  Navigator.of(context).pop();
                  onCancel();
                } else {
                  Navigator.of(context).pop(false);
                }
              },
              padding: EdgeInsets.symmetric(vertical: 12),
              child: Center(child: 
                Text(
                  cancelText,
                  style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.w400,
                   color: theme?.contrastMix3,
                  ),
                ),
              )
            ),
          ),
        ],
      ),
    );
  }
}